home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Programming / Programming Utilities / AfterDark Class / CParticle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-18  |  3.0 KB  |  122 lines  |  [TEXT/KAHL]

  1. /*
  2.  * CParticle is an example CAfterDark subclass. It animates a particle
  3.  * across the screen.  Calculations are made so that the particle's
  4.  * flight doesn't leave the screen.  The animation isn't all that
  5.  * great but it makes for a reasonable example.
  6.  */
  7.  
  8. #include "CParticle.h"
  9.  
  10. /*
  11.  * Initialize seeds the random number generator, gets the value for
  12.  * gravity from the gravity slider, and then generates a set of initial
  13.  * conditions.
  14.  */
  15. OSErr CParticle::Init(RgnHandle blankRgn, GMParamBlockPtr params)
  16. {
  17.     GetDateTime(&randSeed);
  18.     
  19.     G = params->controlValues[GRAVITY_CONTROL];
  20.     this->InitialConditions(params);    
  21.     
  22.     return noErr;
  23. }
  24.  
  25.  
  26. /*
  27.  * Blank uses the superclasses blank method to blank the screen.  It
  28.  * then translates the origin for easier animation.
  29.  */
  30. OSErr CParticle::Blank(RgnHandle blankRgn, GMParamBlockPtr params)
  31. {
  32.     inherited::Blank(blankRgn, params);
  33.     SetOrigin(0,-params->monitors->monitorList[0].bounds.bottom);
  34.         
  35.     return noErr;
  36. }
  37.  
  38.  
  39. /*
  40.  * Draw performs animation by erasing the old particle at position
  41.  * (X,Y), recalculating a new (X,Y) and then checks to see if the
  42.  * particle has "fallen off" the bottom of the screen.  If it has,
  43.  * a new set of initial conditions is generated.  If not, "time"
  44.  * is incremented.
  45.  */
  46. OSErr CParticle::DrawFrame(RgnHandle blankRgn, GMParamBlockPtr params)
  47. {
  48.     Rect aRect;
  49.     
  50.     /*
  51.      * Erase old particle
  52.      */
  53.     SetRect(&aRect, (int)X-4, (int)-Y-4, (int)X+4, (int)-Y+4);
  54.     FillOval(&aRect, params->qdGlobalsCopy->qdBlack);
  55.  
  56.     /*
  57.      * Calculate new particle position
  58.      */
  59.     Y = -G*T*T/2 + Vyo*T + Yo;
  60.     X = Vxo*T + Xo;
  61.     
  62.     /*
  63.      * If the particle is off the screen then generate a new set
  64.      * of initial conditions otherwise draw the particle at the
  65.      * new position.
  66.      */
  67.     if (Y < 0)
  68.         this->InitialConditions(params);
  69.     else {
  70.         T += params->controlValues[DELTAT_CONTROL] / 100.0;
  71.         
  72.         SetRect(&aRect, (int)X-3, (int)-Y-3, (int)X+3, (int)-Y+3);
  73.         FillOval(&aRect, params->qdGlobalsCopy->qdWhite);
  74.     }
  75.  
  76.     return noErr;
  77. }
  78.  
  79.  
  80. /*
  81.  * Move the origin to the normal location and then uses the superclasses
  82.  * blank method to clear the screen.
  83.  */
  84. OSErr CParticle::Close(RgnHandle blankRgn, GMParamBlockPtr params)
  85. {
  86.     SetOrigin(0,0);
  87.     inherited::Blank(blankRgn, params);
  88.         
  89.     return noErr;
  90. }
  91.  
  92.  
  93. /*
  94.  * InitialConditions generates a set of random initial conditions
  95.  * such that the particle will not fly beyond the top of the monitor
  96.  * nor will it go beyond the sides of the monitor during its flight.
  97.  */
  98. void CParticle::InitialConditions(GMParamBlockPtr params)
  99. {
  100.     float maxX, maxY;
  101.     int monitorX, monitorY, randomValue;
  102.         
  103.     monitorX = params->monitors->monitorList[0].bounds.right;
  104.     monitorY = params->monitors->monitorList[0].bounds.bottom;
  105.     
  106.     T = X = Y = 0.0;
  107.     
  108.     do {
  109.         randomValue = Random();
  110.         randomValue = ABS(randomValue);
  111.         Xo = randomValue % monitorX;
  112.         Yo = 0.0;
  113.         
  114.         Vxo = Random() % params->controlValues[MAXVXO_CONTROL];
  115.         randomValue = Random();
  116.         randomValue = ABS(randomValue);
  117.         Vyo = randomValue % params->controlValues[MAXVYO_CONTROL];
  118.  
  119.         maxX = 2*Vyo*Vxo/G + Xo;
  120.         maxY = Vyo*Vyo/2/G + Yo;
  121.     } while (maxY > monitorY || maxX < 0 || maxX > monitorX);
  122. }